| Conditions | 21 |
| Total Lines | 402 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like angular-loader.js ➔ setupModuleLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /** |
||
| 215 | function setupModuleLoader(window) { |
||
| 216 | |||
| 217 | var $injectorMinErr = minErr('$injector'); |
||
| 218 | var ngMinErr = minErr('ng'); |
||
| 219 | |||
| 220 | function ensure(obj, name, factory) { |
||
| 221 | return obj[name] || (obj[name] = factory()); |
||
| 222 | } |
||
| 223 | |||
| 224 | var angular = ensure(window, 'angular', Object); |
||
| 225 | |||
| 226 | // We need to expose `angular.$$minErr` to modules such as `ngResource` that reference it during bootstrap |
||
| 227 | angular.$$minErr = angular.$$minErr || minErr; |
||
| 228 | |||
| 229 | return ensure(angular, 'module', function() { |
||
| 230 | /** @type {Object.<string, angular.Module>} */ |
||
| 231 | var modules = {}; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @ngdoc function |
||
| 235 | * @name angular.module |
||
| 236 | * @module ng |
||
| 237 | * @description |
||
| 238 | * |
||
| 239 | * The `angular.module` is a global place for creating, registering and retrieving AngularJS |
||
| 240 | * modules. |
||
| 241 | * All modules (AngularJS core or 3rd party) that should be available to an application must be |
||
| 242 | * registered using this mechanism. |
||
| 243 | * |
||
| 244 | * Passing one argument retrieves an existing {@link angular.Module}, |
||
| 245 | * whereas passing more than one argument creates a new {@link angular.Module} |
||
| 246 | * |
||
| 247 | * |
||
| 248 | * # Module |
||
| 249 | * |
||
| 250 | * A module is a collection of services, directives, controllers, filters, and configuration information. |
||
| 251 | * `angular.module` is used to configure the {@link auto.$injector $injector}. |
||
| 252 | * |
||
| 253 | * ```js |
||
| 254 | * // Create a new module |
||
| 255 | * var myModule = angular.module('myModule', []); |
||
| 256 | * |
||
| 257 | * // register a new service |
||
| 258 | * myModule.value('appName', 'MyCoolApp'); |
||
| 259 | * |
||
| 260 | * // configure existing services inside initialization blocks. |
||
| 261 | * myModule.config(['$locationProvider', function($locationProvider) { |
||
| 262 | * // Configure existing providers |
||
| 263 | * $locationProvider.hashPrefix('!'); |
||
| 264 | * }]); |
||
| 265 | * ``` |
||
| 266 | * |
||
| 267 | * Then you can create an injector and load your modules like this: |
||
| 268 | * |
||
| 269 | * ```js |
||
| 270 | * var injector = angular.injector(['ng', 'myModule']) |
||
| 271 | * ``` |
||
| 272 | * |
||
| 273 | * However it's more likely that you'll just use |
||
| 274 | * {@link ng.directive:ngApp ngApp} or |
||
| 275 | * {@link angular.bootstrap} to simplify this process for you. |
||
| 276 | * |
||
| 277 | * @param {!string} name The name of the module to create or retrieve. |
||
| 278 | * @param {!Array.<string>=} requires If specified then new module is being created. If |
||
| 279 | * unspecified then the module is being retrieved for further configuration. |
||
| 280 | * @param {Function=} configFn Optional configuration function for the module. Same as |
||
| 281 | * {@link angular.Module#config Module#config()}. |
||
| 282 | * @returns {angular.Module} new module with the {@link angular.Module} api. |
||
| 283 | */ |
||
| 284 | return function module(name, requires, configFn) { |
||
| 285 | |||
| 286 | var info = {}; |
||
| 287 | |||
| 288 | var assertNotHasOwnProperty = function(name, context) { |
||
| 289 | if (name === 'hasOwnProperty') { |
||
| 290 | throw ngMinErr('badname', 'hasOwnProperty is not a valid {0} name', context); |
||
| 291 | } |
||
| 292 | }; |
||
| 293 | |||
| 294 | assertNotHasOwnProperty(name, 'module'); |
||
| 295 | if (requires && modules.hasOwnProperty(name)) { |
||
| 296 | modules[name] = null; |
||
| 297 | } |
||
| 298 | return ensure(modules, name, function() { |
||
| 299 | if (!requires) { |
||
| 300 | throw $injectorMinErr('nomod', 'Module \'{0}\' is not available! You either misspelled ' + |
||
| 301 | 'the module name or forgot to load it. If registering a module ensure that you ' + |
||
| 302 | 'specify the dependencies as the second argument.', name); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** @type {!Array.<Array.<*>>} */ |
||
| 306 | var invokeQueue = []; |
||
| 307 | |||
| 308 | /** @type {!Array.<Function>} */ |
||
| 309 | var configBlocks = []; |
||
| 310 | |||
| 311 | /** @type {!Array.<Function>} */ |
||
| 312 | var runBlocks = []; |
||
| 313 | |||
| 314 | var config = invokeLater('$injector', 'invoke', 'push', configBlocks); |
||
| 315 | |||
| 316 | /** @type {angular.Module} */ |
||
| 317 | var moduleInstance = { |
||
| 318 | // Private state |
||
| 319 | _invokeQueue: invokeQueue, |
||
| 320 | _configBlocks: configBlocks, |
||
| 321 | _runBlocks: runBlocks, |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @ngdoc method |
||
| 325 | * @name angular.Module#info |
||
| 326 | * @module ng |
||
| 327 | * |
||
| 328 | * @param {Object=} info Information about the module |
||
| 329 | * @returns {Object|Module} The current info object for this module if called as a getter, |
||
| 330 | * or `this` if called as a setter. |
||
| 331 | * |
||
| 332 | * @description |
||
| 333 | * Read and write custom information about this module. |
||
| 334 | * For example you could put the version of the module in here. |
||
| 335 | * |
||
| 336 | * ```js |
||
| 337 | * angular.module('myModule', []).info({ version: '1.0.0' }); |
||
| 338 | * ``` |
||
| 339 | * |
||
| 340 | * The version could then be read back out by accessing the module elsewhere: |
||
| 341 | * |
||
| 342 | * ``` |
||
| 343 | * var version = angular.module('myModule').info().version; |
||
| 344 | * ``` |
||
| 345 | * |
||
| 346 | * You can also retrieve this information during runtime via the |
||
| 347 | * {@link $injector#modules `$injector.modules`} property: |
||
| 348 | * |
||
| 349 | * ```js |
||
| 350 | * var version = $injector.modules['myModule'].info().version; |
||
| 351 | * ``` |
||
| 352 | */ |
||
| 353 | info: function(value) { |
||
| 354 | if (isDefined(value)) { |
||
| 355 | if (!isObject(value)) throw ngMinErr('aobj', 'Argument \'{0}\' must be an object', 'value'); |
||
| 356 | info = value; |
||
| 357 | return this; |
||
| 358 | } |
||
| 359 | return info; |
||
| 360 | }, |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @ngdoc property |
||
| 364 | * @name angular.Module#requires |
||
| 365 | * @module ng |
||
| 366 | * |
||
| 367 | * @description |
||
| 368 | * Holds the list of modules which the injector will load before the current module is |
||
| 369 | * loaded. |
||
| 370 | */ |
||
| 371 | requires: requires, |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @ngdoc property |
||
| 375 | * @name angular.Module#name |
||
| 376 | * @module ng |
||
| 377 | * |
||
| 378 | * @description |
||
| 379 | * Name of the module. |
||
| 380 | */ |
||
| 381 | name: name, |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @ngdoc method |
||
| 386 | * @name angular.Module#provider |
||
| 387 | * @module ng |
||
| 388 | * @param {string} name service name |
||
| 389 | * @param {Function} providerType Construction function for creating new instance of the |
||
| 390 | * service. |
||
| 391 | * @description |
||
| 392 | * See {@link auto.$provide#provider $provide.provider()}. |
||
| 393 | */ |
||
| 394 | provider: invokeLaterAndSetModuleName('$provide', 'provider'), |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @ngdoc method |
||
| 398 | * @name angular.Module#factory |
||
| 399 | * @module ng |
||
| 400 | * @param {string} name service name |
||
| 401 | * @param {Function} providerFunction Function for creating new instance of the service. |
||
| 402 | * @description |
||
| 403 | * See {@link auto.$provide#factory $provide.factory()}. |
||
| 404 | */ |
||
| 405 | factory: invokeLaterAndSetModuleName('$provide', 'factory'), |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @ngdoc method |
||
| 409 | * @name angular.Module#service |
||
| 410 | * @module ng |
||
| 411 | * @param {string} name service name |
||
| 412 | * @param {Function} constructor A constructor function that will be instantiated. |
||
| 413 | * @description |
||
| 414 | * See {@link auto.$provide#service $provide.service()}. |
||
| 415 | */ |
||
| 416 | service: invokeLaterAndSetModuleName('$provide', 'service'), |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @ngdoc method |
||
| 420 | * @name angular.Module#value |
||
| 421 | * @module ng |
||
| 422 | * @param {string} name service name |
||
| 423 | * @param {*} object Service instance object. |
||
| 424 | * @description |
||
| 425 | * See {@link auto.$provide#value $provide.value()}. |
||
| 426 | */ |
||
| 427 | value: invokeLater('$provide', 'value'), |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @ngdoc method |
||
| 431 | * @name angular.Module#constant |
||
| 432 | * @module ng |
||
| 433 | * @param {string} name constant name |
||
| 434 | * @param {*} object Constant value. |
||
| 435 | * @description |
||
| 436 | * Because the constants are fixed, they get applied before other provide methods. |
||
| 437 | * See {@link auto.$provide#constant $provide.constant()}. |
||
| 438 | */ |
||
| 439 | constant: invokeLater('$provide', 'constant', 'unshift'), |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @ngdoc method |
||
| 443 | * @name angular.Module#decorator |
||
| 444 | * @module ng |
||
| 445 | * @param {string} name The name of the service to decorate. |
||
| 446 | * @param {Function} decorFn This function will be invoked when the service needs to be |
||
| 447 | * instantiated and should return the decorated service instance. |
||
| 448 | * @description |
||
| 449 | * See {@link auto.$provide#decorator $provide.decorator()}. |
||
| 450 | */ |
||
| 451 | decorator: invokeLaterAndSetModuleName('$provide', 'decorator', configBlocks), |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @ngdoc method |
||
| 455 | * @name angular.Module#animation |
||
| 456 | * @module ng |
||
| 457 | * @param {string} name animation name |
||
| 458 | * @param {Function} animationFactory Factory function for creating new instance of an |
||
| 459 | * animation. |
||
| 460 | * @description |
||
| 461 | * |
||
| 462 | * **NOTE**: animations take effect only if the **ngAnimate** module is loaded. |
||
| 463 | * |
||
| 464 | * |
||
| 465 | * Defines an animation hook that can be later used with |
||
| 466 | * {@link $animate $animate} service and directives that use this service. |
||
| 467 | * |
||
| 468 | * ```js |
||
| 469 | * module.animation('.animation-name', function($inject1, $inject2) { |
||
| 470 | * return { |
||
| 471 | * eventName : function(element, done) { |
||
| 472 | * //code to run the animation |
||
| 473 | * //once complete, then run done() |
||
| 474 | * return function cancellationFunction(element) { |
||
| 475 | * //code to cancel the animation |
||
| 476 | * } |
||
| 477 | * } |
||
| 478 | * } |
||
| 479 | * }) |
||
| 480 | * ``` |
||
| 481 | * |
||
| 482 | * See {@link ng.$animateProvider#register $animateProvider.register()} and |
||
| 483 | * {@link ngAnimate ngAnimate module} for more information. |
||
| 484 | */ |
||
| 485 | animation: invokeLaterAndSetModuleName('$animateProvider', 'register'), |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @ngdoc method |
||
| 489 | * @name angular.Module#filter |
||
| 490 | * @module ng |
||
| 491 | * @param {string} name Filter name - this must be a valid AngularJS expression identifier |
||
| 492 | * @param {Function} filterFactory Factory function for creating new instance of filter. |
||
| 493 | * @description |
||
| 494 | * See {@link ng.$filterProvider#register $filterProvider.register()}. |
||
| 495 | * |
||
| 496 | * <div class="alert alert-warning"> |
||
| 497 | * **Note:** Filter names must be valid AngularJS {@link expression} identifiers, such as `uppercase` or `orderBy`. |
||
| 498 | * Names with special characters, such as hyphens and dots, are not allowed. If you wish to namespace |
||
| 499 | * your filters, then you can use capitalization (`myappSubsectionFilterx`) or underscores |
||
| 500 | * (`myapp_subsection_filterx`). |
||
| 501 | * </div> |
||
| 502 | */ |
||
| 503 | filter: invokeLaterAndSetModuleName('$filterProvider', 'register'), |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @ngdoc method |
||
| 507 | * @name angular.Module#controller |
||
| 508 | * @module ng |
||
| 509 | * @param {string|Object} name Controller name, or an object map of controllers where the |
||
| 510 | * keys are the names and the values are the constructors. |
||
| 511 | * @param {Function} constructor Controller constructor function. |
||
| 512 | * @description |
||
| 513 | * See {@link ng.$controllerProvider#register $controllerProvider.register()}. |
||
| 514 | */ |
||
| 515 | controller: invokeLaterAndSetModuleName('$controllerProvider', 'register'), |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @ngdoc method |
||
| 519 | * @name angular.Module#directive |
||
| 520 | * @module ng |
||
| 521 | * @param {string|Object} name Directive name, or an object map of directives where the |
||
| 522 | * keys are the names and the values are the factories. |
||
| 523 | * @param {Function} directiveFactory Factory function for creating new instance of |
||
| 524 | * directives. |
||
| 525 | * @description |
||
| 526 | * See {@link ng.$compileProvider#directive $compileProvider.directive()}. |
||
| 527 | */ |
||
| 528 | directive: invokeLaterAndSetModuleName('$compileProvider', 'directive'), |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @ngdoc method |
||
| 532 | * @name angular.Module#component |
||
| 533 | * @module ng |
||
| 534 | * @param {string|Object} name Name of the component in camelCase (i.e. `myComp` which will match `<my-comp>`), |
||
| 535 | * or an object map of components where the keys are the names and the values are the component definition objects. |
||
| 536 | * @param {Object} options Component definition object (a simplified |
||
| 537 | * {@link ng.$compile#directive-definition-object directive definition object}) |
||
| 538 | * |
||
| 539 | * @description |
||
| 540 | * See {@link ng.$compileProvider#component $compileProvider.component()}. |
||
| 541 | */ |
||
| 542 | component: invokeLaterAndSetModuleName('$compileProvider', 'component'), |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @ngdoc method |
||
| 546 | * @name angular.Module#config |
||
| 547 | * @module ng |
||
| 548 | * @param {Function} configFn Execute this function on module load. Useful for service |
||
| 549 | * configuration. |
||
| 550 | * @description |
||
| 551 | * Use this method to configure services by injecting their |
||
| 552 | * {@link angular.Module#provider `providers`}, e.g. for adding routes to the |
||
| 553 | * {@link ngRoute.$routeProvider $routeProvider}. |
||
| 554 | * |
||
| 555 | * Note that you can only inject {@link angular.Module#provider `providers`} and |
||
| 556 | * {@link angular.Module#constant `constants`} into this function. |
||
| 557 | * |
||
| 558 | * For more about how to configure services, see |
||
| 559 | * {@link providers#provider-recipe Provider Recipe}. |
||
| 560 | */ |
||
| 561 | config: config, |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @ngdoc method |
||
| 565 | * @name angular.Module#run |
||
| 566 | * @module ng |
||
| 567 | * @param {Function} initializationFn Execute this function after injector creation. |
||
| 568 | * Useful for application initialization. |
||
| 569 | * @description |
||
| 570 | * Use this method to register work which should be performed when the injector is done |
||
| 571 | * loading all modules. |
||
| 572 | */ |
||
| 573 | run: function(block) { |
||
| 574 | runBlocks.push(block); |
||
| 575 | return this; |
||
| 576 | } |
||
| 577 | }; |
||
| 578 | |||
| 579 | if (configFn) { |
||
| 580 | config(configFn); |
||
| 581 | } |
||
| 582 | |||
| 583 | return moduleInstance; |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param {string} provider |
||
| 587 | * @param {string} method |
||
| 588 | * @param {String=} insertMethod |
||
| 589 | * @returns {angular.Module} |
||
| 590 | */ |
||
| 591 | function invokeLater(provider, method, insertMethod, queue) { |
||
| 592 | if (!queue) queue = invokeQueue; |
||
| 593 | return function() { |
||
| 594 | queue[insertMethod || 'push']([provider, method, arguments]); |
||
| 595 | return moduleInstance; |
||
| 596 | }; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param {string} provider |
||
| 601 | * @param {string} method |
||
| 602 | * @returns {angular.Module} |
||
| 603 | */ |
||
| 604 | function invokeLaterAndSetModuleName(provider, method, queue) { |
||
| 605 | if (!queue) queue = invokeQueue; |
||
| 606 | return function(recipeName, factoryFunction) { |
||
| 607 | if (factoryFunction && isFunction(factoryFunction)) factoryFunction.$$moduleName = name; |
||
| 608 | queue.push([provider, method, arguments]); |
||
| 609 | return moduleInstance; |
||
| 610 | }; |
||
| 611 | } |
||
| 612 | }); |
||
| 613 | }; |
||
| 614 | }); |
||
| 615 | |||
| 616 | } |
||
| 617 | |||
| 639 |